﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoClipNameHelper
{
    public class HintData
    {
        public static string HINT_FILE_NAME = ".ClipPlayer.hint";
        //================================================================
        public static Dictionary<string, string> Load(string dirName)
        {
            Dictionary<string, string> hintData = new Dictionary<string, string>();
            //
            try
            {
                if (!System.IO.Directory.Exists(dirName))
                {
                    return hintData;
                }
                string hintFile = System.IO.Path.Combine(dirName, HINT_FILE_NAME);
                if (!System.IO.File.Exists(hintFile))
                {
                    loadHintDefaults(hintData);
                    return hintData;
                }
                if(!GeneralConfig.ParseConfigLines(System.IO.File.ReadLines(hintFile, Encoding.UTF8).ToList<string>(), hintData))
                {
                    hintData.Clear();
                }
            }
            catch(Exception e)
            {
                CLogger.Write(e.Message);
                hintData.Clear();
                return hintData;
            }
            //
            return hintData;
        }
        //================================================================
        private static void loadHintDefaults(Dictionary<string, string> hintData)
        {
            hintData.Clear();
            hintData["Random".ToLower()] = "false";

        }
        //================================================================
        public static string CreateDefaultHintText(bool valueOfRandom, KeyValuePair<string,string>[] kv)
        {
            Dictionary<string, string> hintData = new Dictionary<string, string>();
            hintData["Random".ToLower()] = valueOfRandom ? "true".ToLower() : "false".ToLower();
            //
            foreach(KeyValuePair<string,string> x in kv)
            {
                hintData.Add(x.Key, x.Value);
            }
            //
            string s = "";
            foreach(var x in hintData)
            {
                s += (x.Key + "=" + x.Value + Environment.NewLine);
            }
            return s;
        }
        //================================================================
        public static string WriteHintFile(string directory, bool valueOfRandom, params KeyValuePair<string, string>[] kv)
        {
            //
            try
            {
                string s = CreateDefaultHintText(valueOfRandom, kv);
                System.IO.File.WriteAllText(System.IO.Path.Combine(directory, HINT_FILE_NAME), s, Encoding.UTF8);
            }
            catch (Exception e)
            {
                string eMsg = string.Format("ERROR writeHintFile({0}) {1},{2}", directory, e.Message, e.Source);
                Console.WriteLine(eMsg);
                CLogger.Write(eMsg);
                return eMsg;
            }
            return "";
        }
        //================================================================
    }//class
}//namespace
